import React, { Component } from "react";
import { Link, Route, Switch, Redirect } from "react-router-dom";

class Home extends Component {
  render() {
    return <div>这是首页页面</div>;
  }
}

class About extends Component {
  render() {
    console.log(this.props.match.params);
    return <div>这是关于页面</div>;
  }
}

class Mine extends Component {
  render() {
    // console.log(this.props);
    const {
      location: { search },
    } = this.props;
    // console.log(search);

    const params = new URLSearchParams(search);
    console.log(params.get("a"));
    console.log(params.get("b"));

    return <div>这是我的页面</div>;
  }
}

class Detail extends Component {
  render() {
    console.log(this.props);
    return <div>这是详情页面</div>;
  }
}

class App extends Component {
  render() {
    return (
      <>
        <h2>react-router-参数</h2>
        <ul>
          <li>
            <Link to="/home">home</Link>
          </li>
          <li>
            {/* params传参 */}
            <Link to="/about/123">about</Link>
          </li>
          <li>
            {/* query传参 */}
            <Link to="/mine?a=3&b=4">mine</Link>
          </li>
          <li>
            <Link
              to={{
                pathname: "/detail",
                search: "?x=10&y=20",
                hash: "#lll",
                // state可以写任意对象
                state: { str: "hello" },
              }}
            >
              detail
            </Link>
          </li>
        </ul>

        <hr />

        <Switch>
          <Redirect from="/" to="/home" exact></Redirect>
          <Route path="/home" component={Home}></Route>
          <Route path="/about/:id" component={About}></Route>
          <Route path="/mine" component={Mine}></Route>
          <Route path="/detail" component={Detail}></Route>
        </Switch>
      </>
    );
  }
}

export default App;
